package com.way.common.core.domain;

import com.way.common.core.enums.ResultCode;
import lombok.Getter;

import java.util.Date;

@Getter
public class R<T> {

    private Integer code;
    private String messge;
    private T data;
    private Date timpstamp;

    public static <T> R<T> ok() {
        return build(null, ResultCode.SUCCESS);
    }

    public static <T> R<T> ok(T data) {
        return build(data, ResultCode.SUCCESS);
    }

    public static <T> R<T> fail(ResultCode resultCode) {
        return build(null, resultCode);
    }

    private static <T> R<T> build(T data, ResultCode resultCode) {
        return build(data, resultCode.getCode(), resultCode.getMessage());
    }

    private static <T> R<T> build(T data, Integer code, String message) {
        R<T> result = new R<>();
        result.data = data;
        result.code = code;
        result.messge = message;
        result.timpstamp = new Date();
        return result;
    }

}
